﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Text;
using System.Xml;
using System.Collections;
    /// <summary>
    /// Summary description for rss.
    /// </summary>
public partial class SiteMap : Page
{

    StringBuilder sb = null, sb_header = null;
    private Int32 iProceededItems = 0;
    private ArrayList arrResult=new ArrayList();
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable DT = Dal.ExecuteDBsp("Admin_GetNewsForSiteMap");

        sb = new StringBuilder();
        double startCounter = Convert.ToDouble(DateTime.Now.TimeOfDay.TotalSeconds);
        String sTimeStamp = String.Format("{0}-{1}-{2}", DateTime.Now.Year, DateTime.Now.Month.ToString().PadLeft(2, '0'), DateTime.Now.Day.ToString().PadLeft(2, '0'));
        
        if (DT != null)
            if (DT.Rows.Count > 0)
            {
                for (Int32 i = 0; i < DT.Rows.Count; i++)
                {
                    sb.Append(String.Format("<url>"));
                    sb.Append(String.Format("<loc>{0}</loc>", GetLink(DT.Rows[i])));
                    sb.Append(String.Format("<lastmod>{0}</lastmod>", sTimeStamp));
                    sb.Append(String.Format("<mobile:mobile />"));
                    sb.Append(String.Format("</url>"));
                    iProceededItems++;
                }
            }
        if (sb.Length > 0)
        {
            try
            {
                RSS_Header();
                RSS_Footer();

                XmlDocument xDoc = new XmlDocument();
                xDoc.LoadXml(sb.ToString());
                string sSiteMapPath = Server.MapPath("~/siteMap/");
                xDoc.Save(String.Format(@"{0}SiteMap.xml", sSiteMapPath));
                
                double endCounter = Convert.ToDouble(DateTime.Now.TimeOfDay.TotalSeconds);
                double totalSeconds = endCounter - startCounter;
                arrResult.Add(String.Format("Sitemap : proceeded of {0} items", iProceededItems));
            }
            catch { }
            ShowResult(); 
        }
    }

    /// <summary>
    /// RSS Header
    /// </summary>
    /// <returns></returns>
    private void RSS_Header()
    {
        sb_header = new StringBuilder();
        sb_header.Append("<?xml version='1.0' encoding='UTF-8'?>");
        sb_header.Append("<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:mobile=\"http://www.google.com/schemas/sitemap-mobile/1.0\">");
        sb.Insert(0, sb_header.ToString());
    }

    /// <summary>
    /// RSS Footer
    /// </summary>
    /// <returns></returns>
    private void RSS_Footer()
    {
        sb.Append("</urlset>");
    }

    /// <summary>
    /// Shows the result.
    /// </summary>
    /// <param name="bValue">if set to <c>true</c> [b value].</param>
    private void ShowResult()
    {
        Response.Clear();
        Response.Write("<?xml version='1.0' encoding='utf-8'?>");
        Response.Write("<root>");
        Response.Write("<results>");

        for (Int32 i = 0; i < arrResult.Count; i++)
        {
            Response.Write("<result>" + arrResult[i] + "</result>");
        }
        Response.Write("</results>");
        Response.Write("</root>");
        Response.End();
    }

    private string GetLink(DataRow DR)
    {
        string sRetString = "http://www.medserver.co.il/";
        int iAncestorID = Convert.ToInt32(DR["ancestorID"]);
        NewsType newsType=NewsType.news;

        switch (iAncestorID)
        {
            case 3:
                newsType = NewsType.Wiki;
                break;
            case 4:
                newsType = NewsType.MedicalCenters;
                break;
            case 8:
                newsType = NewsType.hotels;
                break;
            default:
                int iPID = Convert.ToInt32(DR["PID"]);
                if (iPID == 3)
                    newsType = NewsType.Wiki;
                break;
        }


        switch (newsType)
        {
            case NewsType.news:
            case NewsType.popular:
                sRetString += string.Format("{0}-a.aspx", DR["cPageName"].ToString());
                break;
            case NewsType.deadSea:
                sRetString += string.Format("deadsea.aspx");
                break;
            case NewsType.aboutUS:
                sRetString += string.Format("about.aspx");
                break;
            case NewsType.Wiki:
                sRetString += string.Format("{0}-wa.aspx", DR["cPageName"].ToString());
                break;
            case NewsType.MedicalCenters:
                sRetString += string.Format("{0}-ca.aspx", DR["cPageName"].ToString());
                break;
            case NewsType.MedicalTourism:
                sRetString += string.Format("{0}-a.aspx", DR["cPageName"].ToString());
                break;
            case NewsType.hotels:
                sRetString += string.Format("{0}-a.aspx", DR["cPageName"].ToString());
                break;
        }
        return sRetString;
    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
        //
        // CODEGEN: This call is required by the ASP.NET Web Form Designer.
        //
        InitializeComponent();
        base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.Load += new EventHandler(this.Page_Load);
    }
    #endregion
}


